/**
 * 自定义下拉框
 * 2013-10-9
 */
/**
 * 提供外部接口调用
 * */
var KSELECT = {
		loadSelect:loadSelect,    					//加载下拉框（单个）
		loadSelectAll:loadSelectAll,				//加载下拉框（全部，且class为kSelect）
		kSelectSelected:kSelectSelected,			//下拉框选中某个值
		addOption:kSelectAddOption,					//增加选项
		removeOption:kSelectRemoveOption,			//删除选项
		moveOptionToEnd:kSelectMoveOption,			//移动选项到最后
		getSelectedValue:getSelectedValue,			//获取选中的值
		getSelectedText:getSelectedText,			//获取选中的文本
		clearOption:clearOption,					//清空下拉框中的选项
		resetOption:resetOption,					//重置下拉框选项
		closeOption:closeOption						//关闭下拉框
};

/**全局变量,需要用到的全局变量都放入GLOBAL里
 * 添加GLOBAL全部变量方法
 * GLOBAL.namespace(str);
 * 参数：	str 为定义的全局变量
 * 示例:	GLOBAL.namespace("A.name");
 * 		GLOBAL.A.name = "全局变量" ;
 * */
var GLOBAL = {
		dom:{},
		select:new Array()//下拉框对象栈
};

GLOBAL.namespace = function(str){
	var arr = str.split("."),o = GLOBAL;
	for(var i = ( arr[0]=="GLOBAL" ? 1:0 ); i < arr.length; i++){
		o[arr[i]] = o[arr[i]] || {};
		o = o[arr[i]];
	}
};
//下拉框id，如果下拉框没有id，则帮给它一个id值，为了不重复，1000为初始值，每用一个id，selectId的值+1
GLOBAL.selectId = 1000;
/**
 * 根据类名获得对象
 * 调用方法：var topicnum=getElementsByClassName("classname");
 */
GLOBAL.dom.getElementsByClassName = function(searchClass, node,tag){  
	if(document.getElementsByClassName){return  document.getElementsByClassName(searchClass);}
	else{        
		node = node || document;        
		tag = tag || "*";        
		var classes = searchClass.split(" "),        
		elements = (tag === "*" && node.all)? node.all : node.getElementsByTagName(tag),        
		patterns = [],         
		returnElements = [],        
		current,         
		match;        
		var i = classes.length;       
		while(--i >= 0){patterns.push(new RegExp("(^|\\s)" + classes[i] + "(\\s|$)"));}        
		var j = elements.length;       
		while(--j >= 0){            
			current = elements[j];           
			match = false;            
			for(var k=0, kl=patterns.length; k<kl; k++){                
				match = patterns[k].test(current.className);                
				if (!match)  break;           
			} 
			if (match)  returnElements.push(current);        
		}        
		return returnElements;   
	} 
};
/*
 * 参数继承，待实现
 * GLOBAL.extend = function(){
	
};*/
/**
 * 绑定事件方法
 * node:需要绑定事件的dom对象
 * eventType:事件名，click、mouseover、mouseout等，不需要加"on"
 * handler:需要调用的方法
 * scope :指定this指向，注意：设定scope将不能用removeAttach来取消这个事件
 * */
GLOBAL.attachEvent = function(node,eventType,handler,scope){
	Node = typeof node =="string"?document.getElementById(node) :node;
	if ( scope ){
		if(document.all){
			if( Node ) {
				Node.attachEvent("on"+eventType,function(){
					handler.apply(scope,arguments);
				});
			}
	    }else{
	    	if( Node ){
	    		Node.addEventListener(eventType,function(){
	    			handler.apply(scope,arguments);
	    		},false);
	    	}
	    	
	    }
		
	}else{
		if(document.all){
			if( Node ) {
				Node.attachEvent("on"+eventType,handler);
			}
	    }else{
	    	if( Node ){
	    		Node.addEventListener(eventType,handler,false);
	    	}
	    	
	    }
	}
	
};
/**
 * 删除事件方法
 * obj:需要绑定事件的dom对象
 * event:事件名，click、mouseover、mouseout等，不需要加"on"
 * fun:需要调用的方法
 * */
GLOBAL.removeEvent = function(obj,event,fun){
	if(document.all){
		if( obj ){
			obj.detachEvent("on"+event, fun);
		}
	}else{
		if( obj ){
			obj.removeEventListener(event, fun, false);
		}
	}
};
/**
 * 阻止默认事件
 * */
GLOBAL.stopDefault = function(e) { 
	if ( e && e.preventDefault ){
		e.preventDefault();   
	}
	else  {
		window.event.returnValue = false;
	}    
};
/**
 * 阻止事件冒泡
 * */
GLOBAL.stopPropagation = function(e){
	
	e = window.event || e;
	if(document.all){
		e.cancelBubble = true;
	}else{
		e.stopPropagation();
	}
};
//判断是否ie10
GLOBAL.isIE10 = function (){
	　　var flag = false;
	　　try{
	　　　　if(window.matchMedia("screen and (-ms-high-contrast: active), (-ms-high-contrast: none)").matches){
	　　　　　　flag = true;
	　　　　}
	　　}catch(e){ }

	　　return flag;
	}
/**
 * 获取下拉框位置
 *
 */
GLOBAL.offSet = function (obj){
		var t = obj.offsetTop+obj.offsetHeight;  
	    var l = obj.offsetLeft;  
	    var w = obj.offsetWidth;  
	    var h = obj.offsetHeight; 
	    if( document.all){
	    	t+=1;
	    	if( GLOBAL.isIE10() ){
	    		h+=2;
	    	}
	    	h+=2;
	    }else{
	    	t+=2;
	    	h+=3;
	    } 
	    var obj1,obj2;
	    obj1=obj;
	    obj2=obj;
	    while(obj1=obj1.offsetParent)  
	    {  
	        t+=obj1.offsetTop;  
	        l+=obj1.offsetLeft;  
	    }
	    while( obj2.parentNode ){
		    if( obj2.style.borderLeftWidth ){
		    	l+=parseInt(obj2.style.borderLeftWidth.replace("px",""));
		    }
		    if( obj2.style.borderTopWidth ){
		    	t+=parseInt(obj2.style.borderTopWidth.replace("px",""));
		    }
		    if( obj2.tagName =="FIELDSET"){
		    	if(document.all &&!GLOBAL.isIE10()){
		    		l+=2;
		    	}
		    }
	    	obj2 = obj2.parentNode
		    
	    }
	    return {  
	        top : t,  
	        left : l,  
	        width : w,  
	        height : h  
	    } ;
	};
/**
 * 获取标签自定义属性的值
 * */
GLOBAL.dom.getAttribute = function(obj,attr){
	if( obj ){
		return obj.getAttribute(attr);
	}
};
/**
 * 设置标签自定义属性的值
 */
GLOBAL.dom.setAttribute = function(obj,attr,value){
	if( obj ){
		return obj.setAttribute(attr,value);
	}
};
/**
 * 去除dom属性
 */
GLOBAL.dom.removeAttr = function(obj,attr){
	if( obj ){
		return obj.removeAttribute(attr);
	}
};

/*-------------------------------------------------下拉框对象---------------------------------------------------------------------
 * 对象名：DivSelect
 * 描述：实现HTML的<select>
 * 
 * 对象名：DivOption
 * 描述:实现HTML的<option>
 */
function DivSelect(){
	var id = null ;
	var options ={};
	var dom =null;
	var selectImageDom =null;
	//下拉框的option对象
	var divOption =new DivOption();
	//title
	var title = null;
	//select的css样式类
	this.className = "divSelect" ;
	//html类型下拉框的css
	this.htmlClassName ="divSelectHtml";
	this.selectAttr = {
			build :"build",                 //是否构建过下拉框
			selectTypeAttr:"selectType",    //下拉框类型
			selectWidthAttr:"selectWidth",  //下拉框宽度
			optionWidthAttr:"optionWidth",  //选择框宽度
			titleAttr:"ktitle",				//下拉框标题
			scrollSizeAttr:"scrollSize",	//启用滚动条的条件
			onchange:"onchange"				//change事件
	};
	//下拉框内容层css样式类
	this.selectContentClassName = "divSelectContent";
	this.selectContentTextClassName = "divSelectContentText";
	//内容层里A标签样式类
	this.selectContentClassNameByA = "selectContentA";
	this.htmlTypeSelectContentClassNameByA = "divSelectHtml_selectContentA";
	//下拉框图片层css样式类
	this.selectImageDownArrowClassName = "divSelectDownArrowImage";
	this.selectImageDoubleArrowClassName = "divSelectDoubleArrowImage";
	
	/*---------------------------------------------------set方法，用于设置参数-----------------------------------------------*/

	this.setTitle =function(o){
		title = o;
	};
	this.setSelectImageDom = function(o){
		selectImageDom = o;
	};
	this.setDom = function(o){
		dom = o;
	};
	this.setId = function(o){
		id = o;
	};
	this.setDivOption = function(o){
		divOption = o;
	};
	this.setOptions = function(o){
		options = o;
	};
	
	/*---------------------------------------------------get方法，用于获取参数----------------------------------------------*/

	this.getTitle = function(){
		return title;
	};
	this.getId = function(){
		return id;
	};
	this.getTest = function(){
		return test;
	};
	this.getDom = function(){
		return dom;
	};
	this.getOptions = function(){
		return options;
	};
	this.getDivOption = function(){
		return divOption;
	};
	this.getSelectImageDom = function(){
		return selectImageDom ;
	};
}
function DivOption(){
	var optionWidth = {
			widthAuto:true,
			width:"1000px"//由于ie6下存在bug，不能自适应宽度，这里必须先给一个比较大的，然后自己来做自适应宽度
	};
	var options = {};
	var id = "selectchildOf";
	var selectId ="";
	var selectContentDom =null;
	//搜索下拉框
	var selectContentText = {
			enable:false,		//是否开启搜索下拉框模式
			dom:null,			//textfield对象
			className:null  	//样式
	};
	var selectImageDom = null;
	var hiddenDom = null;
	var hiddenDomName = null;
	var optionHtml = {
			dom:null,
			width:null,
			height:null
	};
	//选中的值
	var selectedValue = null;
	//选中的文本
	var selectedText = null;
	//改变选中时触发的方法
	var changeFun = null;
	//改变选中时是否触发事件
	var openChangeFun = false;
	var optionDom = null;
	//下拉框选项数据源,从页面获取
	var optionsDomByPage = null;
	//下拉框选项数据源,从后台获取,暂未实现
	var optionsDomByData = null;
	//下拉框类别，现有三种：event、basic、html
	var optionType = "basic";
	//是否设置下拉框选项前的图标
	var optionIcon = false;
	var selectedOptionLiDom = null;
	var scroll = {
			size:10 //超过10条数据后启用滚动条
	};
	this.isScroll = false;
	this.className = "divOption";
	this.optionLiClassName = "divLi";
	this.optionIconClassName = "divOptionIcon";
	this.underLinedClassName = "underLined";
	this.selectedClassName = "li-mouseover";
	this.unSelectedClassName = "li-mouseout";
	this.bodyDivId = "selectBodyDiv";
	this.windowClickHandler = null;
	//下拉框类别
	this.optionTypes = {
			basic:"basic",
			event:"event",
			html:"html"
	};
	//自定义标签属性
	this.optionAttr = {
			value:"value",
			text:"text",
			iconSrc:"iconSrc",
			underLined:"underLined",
			underLinedWidth:"underLinedWidth",
			underLinedType:"underLinedType"
	};
	
	/*---------------------------------------------------set方法，用于设置参数-----------------------------------------------*/
	this.setSelectContentText = function(o){
		selectContentText = o;
	}
	this.setOptionHtml = function(o){
		optionHtml = o;
	}
	this.setHiddenDomName = function(o){
		hiddenDomName = o;
	}
	this.setOpenChangeFun = function(o){
		openChangeFun = o;
	}
	this.setChangeFun = function(o){
		changeFun = o;
	}
	this.setSelectedValue =function(o){
		selectedValue = o;
	};
	this.setSelectedText =function(o){
		selectedText = o;
	};
	this.setSelectImageDom = function(o){
		selectImageDom = o;
	};
	this.setSelectedOptionLiDom = function(o){
		selectedOptionLiDom = o;
	};
	this.setOptionIcon = function(o){
		optionIcon = o;
	};
	this.setScroll = function(o){
		scroll = o;
	};
	this.setOptionDom = function(o){
		optionDom = o;
	};
	this.setOptionType = function(o){
		optionType = o ;
	};
	this.setHiddenDom = function(o){
		hiddenDom = o;
	};
	this.setOptionsDomByPage = function(o){
		optionsDomByPage = o;
	};
	this.setOptionsDomByData = function(o){
		optionsDomByData = o;
	};
	this.setOptionWidth = function(o){
		optionWidth = o;
	};
	this.setSelectContentDom = function(o){
		selectContentDom = o;
	};
	this.setSelectId = function(o){
		selectId = o;
	};
	this.setId = function(o){
		id += o;
	};
	this.setOptions = function(o){
		options = o;
	};
	
	/*---------------------------------------------------get方法，用于获取参数----------------------------------------------*/
	this.getSelectContentText = function(){
		return selectContentText;
	}
	this.getOptionHtml = function(){
		return optionHtml ;
	}
	this.getHiddenDomName = function(){
		return hiddenDomName ;
	}
	this.getOpenChangeFun = function(){
		return openChangeFun;
	}
	this.getChangeFun = function(){
		return changeFun;
	}
	this.getSelectedValue =function(){
		return selectedValue;
	};
	this.getSelectedText =function(){
		return selectedText;
	};
	this.getSelectImageDom = function(){
		return selectImageDom ;
	};
	this.getSelectedOptionLiDom = function(){
		return selectedOptionLiDom ;
	};
	this.getOptionIcon = function(){
		return optionIcon ;
	};
	this.getScroll = function(){
		return scroll ;
	};
	this.getOptionDom = function(){
		return optionDom ;
	};
	this.getOptionType = function(){
		return optionType  ;
	};
	this.getHiddenDom = function(){
		return hiddenDom ;
	};
	this.getOptionsDomByPage = function(){
		return optionsDomByPage ;
	};
	this.getOptionsDomByData = function(){
		return optionsDomByData ;
	};
	this.getOptions = function(){
		return options;
	};
	this.getId = function(){
		return id ;
	};
	this.getSelectId = function(){
		return selectId ;
	};
	this.getSelectContentDom = function(){
		return selectContentDom ;
	};
	this.getOptionWidth = function(){
		return optionWidth ;
	};
	this.getClassName = function(){
		return this.className;
	};
	
};

/*---------------------------------------------------原型--------------------------------------------------*/
DivSelect.prototype = {
		init:function(){
			//获取所需参数
			var options = this.getOptions();
			//设置css样式
			if ( options.selectDom.className !=this.className ){
				if( options.type == this.getDivOption().optionTypes.html ){
					options.selectDom.className = this.htmlClassName +" "+options.selectDom.className;
				}else{
					options.selectDom.className = this.className +" "+options.selectDom.className;
				}
				
			}
		
			//设置宽度,不设置宽度为自适应宽度
			if ( options.selectWidth!=null ){
				options.selectDom.style.width = options.selectWidth.replace("px","")+"px";
			} 
			if ( options.optionWidth!=null ){
				this.getDivOption().getOptionWidth().width = options.selectWidth.replace("px","")+"px" ;
				this.getDivOption().getOptionWidth().widthAuto = false;
			}
			//设置title
			if ( options.title!=null ){
				this.setTitle(options.title);
			}
			if ( options.type!=null ){
				this.getDivOption().setOptionType( options.type );
			}
			this.setDom(options.selectDom);
		},
		createContent:function(){
			var selectDom = this.getDom();
			var title = this.getTitle() ;
			var selectADom = document.createElement("a");
			var spanContentDom = document.createElement("span");
			var spanImageDom = document.createElement("span");
			if ( GLOBAL.dom.getAttribute(selectDom, "name") ){
				//建立隐藏域，用来提交表单
				var hiddenDom = document.createElement("input");
				hiddenDom.type = "hidden";
				hiddenDom.value = null;
				var hiddenDomName =GLOBAL.dom.getAttribute(selectDom, "name");
				this.getDivOption().setHiddenDom(hiddenDom);
				this.getDivOption().setHiddenDomName(hiddenDomName);
				selectDom.appendChild(hiddenDom);
			}
			//定义css样式
			selectADom.className = this.selectContentClassNameByA;
			spanContentDom.className = this.selectContentClassName;
			if( this.getDivOption().getOptionType()==this.getDivOption().optionTypes.event ){
				spanImageDom.className = this.selectImageDownArrowClassName;
			}else if(this.getDivOption().getOptionType()==this.getDivOption().optionTypes.html){
				spanImageDom.className = this.selectImageDownArrowClassName;
				selectADom.className = this.htmlTypeSelectContentClassNameByA;
			}
			else{
				spanImageDom.className = this.selectImageDoubleArrowClassName;
			}
			selectADom.href ="javascript:;";
			//启用文本框展现方式，内容的展现是否在文本框中
			if( this.getDivOption().getSelectContentText().enable ){
				//如果存在文本节点，则把文本节点塞入spanContenetDom，如果不存在，则创建文本节点
				if( this.getDivOption().getSelectContentText().dom ){
					spanContentDom.appendChild(this.getDivOption().getSelectContentText().dom );
				}else{
					var textDom = document.createElement("input");
					textDom.type= "text";
					textDom.className = this.selectContentTextClassName;
					textDom.value="";
					spanContentDom.appendChild(textDom);
				}
			}
			//组合节点
			selectADom.appendChild(spanContentDom);
			selectDom.appendChild(selectADom);
			selectDom.appendChild(spanImageDom);
			
			this.setSelectImageDom(spanImageDom);
			this.getDivOption().setSelectContentDom(spanContentDom);
			this.getDivOption().setSelectImageDom(spanImageDom);
			
			//填充title
			if ( title ){
				this.getDivOption().changeSelectContent(title);
			}
			//若给定下拉框宽度，则内容部分也需要给定宽度，宽度为下拉框宽度-右侧下拉图片的宽度
			if( this.getOptions().selectWidth ){
				contentWidth = parseInt(this.getOptions().selectWidth.replace("px",""))-20;
				if( contentWidth <0 ){
					contentWidth = 0;
				}
				spanContentDom.style.width = contentWidth+"px";
			}else{
				this.getDivOption().repeatSelectWidth();
				/*//自适应宽度
				this.getDom().style.width = spanContentDom.scrollWidth + spanImageDom.scrollWidth+10+"px";*/
			}
			GLOBAL.dom.setAttribute(this.getDom(), this.selectAttr.build,true);
		},
		addSelectClickEvent : function(){
			//是否存在option
			var selectChildObj = document.getElementById(this.getDivOption().getId());
			var selectObj = this.getDom();
			//关闭其它下拉框
        	var divOptions = GLOBAL.dom.getElementsByClassName(this.getDivOption().className, document.body,"div");
			for( var i = 0; i<divOptions.length;i++){
				if ( divOptions[i].id != this.getDivOption().getId()){
					divOptions[i].style.display = "none";
				}
			}
	        if ( selectChildObj ){  
	        	//判断当前的下拉是不是打开状态，如果是打开的就关闭掉。是关闭的就打开。 
	            if ( selectChildObj.style.display == "none" ){
	            	this.getDivOption().openOption(this.getDivOption().getOptionDom());
	            }else{
	            	this.getDivOption().closeOption(this.getDivOption().getOptionDom());
	            }
	            if ( this.getDivOption().getSelectedOptionLiDom() ){
	  	            //清除选中样式
	            	var optionList = this.getDivOption().getOptionDom().childNodes;
	              	for ( var i=0 ;i<optionList.length;i++ ){
	    	            	this.getDivOption().optionUnSelected(optionList[i]);
	    	            }
	            	this.getDivOption().optionSelected(this.getDivOption().getSelectedOptionLiDom());
	            }
	        }
		},
		createEvent:function(){
			var divSelect = this;
			var bodyMousemove = function(){
				if( divSelect.getDivOption().getOptionType()!=divSelect.getDivOption().optionTypes.html){
					divSelect.getDom().style.backgroundColor="#FCFCFC";
				}
			};
			windowClickHandler = function(){
				//关闭所有下拉框
				var divOptions = GLOBAL.dom.getElementsByClassName(divSelect.getDivOption().className, document.body,"div");
				for( var i = 0; i<divOptions.length;i++){
					divOptions[i].style.display = "none";
				}
				//注销事件
				GLOBAL.removeEvent(window.document, "click", windowClickHandler);
			};
			//注册点击事件用来打开或关闭下拉框
			divSelect.getDom().onclick = function(e){
				//传进来的onclick方法
				if( divSelect.getOptions().selectClickFun !=undefined &&divSelect.getOptions().selectClickFun!=null ){
					divSelect.getOptions().selectClickFun();
				}
				//如果已经有页面点击事件，则先注销该事件
				GLOBAL.removeEvent(window.document, "click", windowClickHandler);
				//阻止双击事件
				GLOBAL.stopDefault(e);
				//阻止冒泡机制
				GLOBAL.stopPropagation(e);
				divSelect.getDivOption().windowClickHandler = windowClickHandler;
				//html下拉框的关闭事件控制
				if( divSelect.getDivOption().getOptionType()!=divSelect.getDivOption().optionTypes.html){
					GLOBAL.attachEvent(window.document, "click", windowClickHandler);
				}
				divSelect.addSelectClickEvent();				
			};
			if (document.all &&!GLOBAL.isIE10()){
				divSelect.getDom().ondblclick = function(e){
					//阻止冒泡机制
					GLOBAL.stopPropagation(e);
					divSelect.getDom().onclick();
				};
			}
		
			
			divSelect.getDom().onmousedown = function(){
				if( divSelect.getDivOption().getOptionType()!=divSelect.getDivOption().optionTypes.html){
					divSelect.getDom().style.backgroundColor="#D3D3D3";
				}
				//如果点击鼠标移动的话，触发不了up事件，以下是采取措施
				GLOBAL.attachEvent(window.document, "mousemove", bodyMousemove);
			};
			divSelect.getDom().onmouseup = function(){
				if( divSelect.getDivOption().getOptionType()!=divSelect.getDivOption().optionTypes.html){
					divSelect.getDom().style.backgroundColor="#FCFCFC";
				}
				GLOBAL.removeEvent(window.document, "mousemove", bodyMousemove);
			};
		
		},
		/**下拉框选项的初始化选择*/
		optionInitSelected:function(){
			//是否需要选中第一个元素
			if( !this.getTitle() ){
				var firstOption  = null;
				if( this.getDivOption().getOptionDom() ){
					if( this.getDivOption().getOptionDom().getElementsByTagName("div")[0]){
						firstOption = GLOBAL.dom.getAttribute(this.getDivOption().getOptionDom().getElementsByTagName("div")[0],"value");
					}
					if( firstOption ){
						this.getDivOption().setSelectedValue(firstOption);
						this.getDivOption().optionSelected(this.getDivOption().selectOptionLi(firstOption));
					}
				}
			}else{
				//this.getDivOption().getSelectContentDom().innerHTML = this.getTitle();
				this.getDivOption().changeSelectContent(this.getTitle());
				if ( this.getDivOption().getHiddenDom() ){
					this.getDivOption().getHiddenDom().value = null;
					this.getDivOption().getHiddenDom().name ="";
				}
				this.getDivOption().setSelectedValue(null);
				this.getDivOption().setSelectedText(null);
				this.getDivOption().setSelectedOptionLiDom(null);
				this.getDivOption().repeatSelectWidth();
			}
		}
};

DivOption.prototype = {
	init : function(){
		//获取divOption
		var selectDom = document.getElementById(this.getSelectId());
		if (selectDom){
			//获取页面的option
			var optionDoms = selectDom.getElementsByTagName("div")==null?false: selectDom.getElementsByTagName("div");
			this.setOptionsDomByPage(optionDoms);
		}
		
		//获取后台list数据（待做）
		//this.setOptionsDomByData()
	},
	createOption : function(){
		var optionObj = this;
		var selectDom = document.getElementById(this.getSelectId());
		//创建div层来显示option
		var divOption = document.createElement("div");
		divOption.id = this.getId();
		divOption.className = this.getClassName();
		divOption.style.width = (this.getOptionWidth().width).replace("px","")+"px";
		var optionDoms = this.getOptionsDomByPage();
		if( !optionDoms ){
			this.setOptionDom(divOption);
			return false;//没有下拉元素
		}
		//构建下拉列表
		var optionType = this.getOptionType();
		switch( optionType ){
			case this.optionTypes.basic :this.createBasicOptionList(divOption,optionDoms);break;
			case this.optionTypes.event :this.createEventOptionList(divOption,optionDoms);break;
			case this.optionTypes.html :this.createHtmlOptionList(divOption);break;
			default :this.createBasicOptionList(divOption,optionDoms);break;
		}
		//给下拉菜单添加事件
		var timeld = null;
		optionMouseout = function(){
			timeld= setTimeout(function(){
				optionObj.closeOption(optionObj.getOptionDom());
			},300);
		};
		optionMouseover = function(){
			clearTimeout(timeld);
		};
	    GLOBAL.attachEvent(divOption, "mouseout",optionMouseout);
	    GLOBAL.attachEvent(divOption, "mouseover",optionMouseover);
	    document.body.appendChild(divOption);
	    //宽度自适应
	    this.repeatOptionWidth(divOption);
		//定位位置
		this.repeatPosition(selectDom, divOption);
		this.setOptionDom(divOption);
	},
	repeatOptionWidth:function(divOption){
		if ( this.getOptionWidth().widthAuto &&this.getOptionType()!=this.optionTypes.html){
			var maxWidth = this.maxWidthByOptionList(divOption);
			var selectWidth = document.getElementById(this.getSelectId()).scrollWidth;
			if ( maxWidth < selectWidth-10 ){
				maxWidth = selectWidth;
				if(document.all &&!GLOBAL.isIE10()){
					maxWidth+=2;
				}
			}else{
				maxWidth+=12;
			}
			divOption.style.width = maxWidth+"px";
		}
	}
	,
	createBasicOptionList:function(divOption,optionDoms){
		var optionObj = this;
		for ( ;optionDoms.length!=0;){
			var optionLi = optionObj.createOptionLi(optionDoms[0], optionObj);
			optionLi.onmouseover = function(){
				this.className = optionObj.selectedClassName;
				var optionList = divOption.childNodes;
            	for ( var i=0 ;i<optionList.length;i++ ){
            		if ( this!=optionList[i] ){
            			optionList[i].onmouseout();
            		}
            	}
			};
			optionLiClick = function(){
				optionObj.optionSelected(this);
				optionObj.closeOption(optionObj.getOptionDom());
			};
			GLOBAL.attachEvent(optionLi, "click", optionLiClick,optionLi);
			optionObj.addOptionUnderLined(optionLi, optionDoms[0], divOption);
		}
	},
	createEventOptionList:function(divOption,optionDoms){
		var optionObj = this;
		for ( ;optionDoms.length!=0;){
			var optionLi = optionObj.createOptionLi(optionDoms[0], optionObj);
			optionLiClick = function(e){
				optionObj.closeOption(optionObj.getOptionDom());
				GLOBAL.stopPropagation(e);
			};
			optionLi.onmouseover = function(){
				this.className = optionObj.selectedClassName;
			};
			GLOBAL.attachEvent(optionLi, "click", optionLiClick);
			optionObj.addOptionUnderLined(optionLi, optionDoms[0], divOption);
			
		}
	},
	/**
	 * 下拉框内容自定义的创建方式
	 * */
	createHtmlOptionList:function(divOption){
		if(this.getOptionHtml().dom &&this.getOptionHtml().width){
			divOption.appendChild(this.getOptionHtml().dom);
			divOption.style.width =this.getOptionHtml().width+"px";
			if(this.getOptionHtml().height!=null){
				divOption.style.height =this.getOptionHtml().height+"px";
			}/*
			高度无法很好的自适应
			else{
				var clearDiv = document.createElement("div");
				clearDiv.style.clear = "both";
				divOption.appendChild(clearDiv);
			}*/
			
		}
	},
	/**
	 * 构建li
	 * */
	createOptionLi :function(optionDom,optionObj){
		var optionLi = document.createElement("div");
		optionLi.className = this.optionLiClassName;
		GLOBAL.dom.setAttribute(optionLi, this.optionAttr.value, GLOBAL.dom.getAttribute(optionDom,this.optionAttr.value));
		GLOBAL.dom.setAttribute(optionLi, this.optionAttr.text,optionDom.innerHTML);
		this.changeEvent(optionDom,optionLi);
		this.createOptionIcon(optionLi, optionDom);
		optionLi.onmouseout = function(){
			this.className = optionObj.unSelectedClassName;
		};
		return optionLi;
	},
	/**
	 * 添加下划线
	 * */
	addOptionUnderLined :function(optionLi,optionDom,divOption){
		var underLined = GLOBAL.dom.getAttribute(optionDom, this.optionAttr.underLined);
		var type = GLOBAL.dom.getAttribute(optionDom, this.optionAttr.underLinedType);
		var width = GLOBAL.dom.getAttribute(optionDom, this.optionAttr.underLinedWidth);
		optionLi.appendChild(optionDom);
		if ( underLined ){
			if( underLined =="up" ){
				this.createOptionUnderLined(divOption,type,width);
				divOption.appendChild(optionLi);
			}else if( underLined =="down" ){
				divOption.appendChild(optionLi);
				this.createOptionUnderLined(divOption);
			}else{
				divOption.appendChild(optionLi);
			}
		}else{
			divOption.appendChild(optionLi);
		}
	},
	/**
	 * 创建列表前的图标
	 * */
	createOptionIcon :function(optionLi,optionListSrc){
		var iconSrc = GLOBAL.dom.getAttribute(optionListSrc,this.optionAttr.iconSrc);
		if ( iconSrc ){
			var imageDom = document.createElement("span");
			imageDom.className = this.optionIconClassName;
			imageDom.style.backgroundImage = "url("+iconSrc+")";
			imageDom.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+iconSrc+"',sizingMethod=image)";
			optionLi.appendChild(imageDom);
			this.setOptionIcon(true);
		}
		
	},
	/**
	 * 创建下划线
	 * */
	createOptionUnderLined :function(divOption,type,width){
		var optionLi = document.createElement("div");
		optionLi.className = this.optionLiClassName;
		var underLinedDivDom = document.createElement("div");
		var underLinedDom = document.createElement("div");
		underLinedDivDom.className = this.underLinedClassName;
		if (this.getOptionIcon() ){
			var imageDom = document.createElement("span");
			imageDom.className = this.optionIconClassName;
			optionLi.appendChild(imageDom);
		}

		if ( type ){
			underLinedDom.style.borderBottomStyle = type;
		}
		if ( width ){
			underLinedDom.style.width = width+"px";
		}
		underLinedDivDom.appendChild(underLinedDom);
		optionLi.appendChild(underLinedDivDom);
		divOption.appendChild(optionLi);
		//取消点击关闭下拉框的事件
		optionLi.onclick = function(e){
			GLOBAL.stopPropagation(e);
		};
	
	},
	/**
	 * 返回下拉列表中最大的宽度
	 * */
	maxWidthByOptionList :function(divOption){
		var maxWidth = 0;
		var optionList = divOption.childNodes;
		for (var i = 0;i<optionList.length;i++){
			var childList = optionList[i].childNodes;
			var childWidth = 0;
			for( var j = 0 ;j<childList.length;j++ ){
				childWidth += childList[j].scrollWidth;
			}
			if( maxWidth < childWidth ){
				maxWidth = childWidth;
			}
		}
		// 如果有滚动条，那宽度必须再增加些
		if(this.isScroll){
			if (document.all){
				maxWidth+=23;
			}else{
				maxWidth+=23;
			}
		}
		if( this.getOptionIcon() ){
			if (document.all){
				maxWidth+=10;
			}else{
				maxWidth+=15;
			}
		}
		return maxWidth;
	},
	/**
	 * 重新定位option位置
	 * selectObj:DIV构建的select对象
	 * optionObj：DIV构建的option对象
	 * */
	repeatPosition:function(selectObj,optionObj){
		if ( this.getScroll().size ){
			if( optionObj.childNodes[0] ){
				var optionMaxHeight = optionObj.childNodes[0].scrollHeight *this.getScroll().size;
				if( optionObj.scrollHeight > optionMaxHeight ){
					optionObj.style.height = optionMaxHeight;
					optionObj.style.overflowY="auto";
					this.isScroll = true;
				}
			}
		} 
		var offset = GLOBAL.offSet(selectObj);
		var bodyHeight =  document.body.clientHeight;
		var bodyWidth  = document.body.clientWidth;
		var selectTop =  offset.top;
		var selectLeft = offset.left;
		var optionWidth = optionObj.scrollWidth;
		var optionHeight = optionObj.scrollHeight;
		if ( optionObj.style.height ){
			optionHeight =optionObj.style.height;
			optionHeight = optionHeight.replace("px","");
			optionHeight = parseInt(optionHeight) ;
		}
		if (!document.all){
			optionHeight +=2;
		}
		if(this.isScroll){
			optionWidth+=15;
		}
		if ( (optionWidth + selectLeft) > bodyWidth ){
			selectLeft -=(optionWidth -offset.width+2);				
		}
		if( (optionHeight + selectTop) > bodyHeight ){
			selectTop -=(optionHeight+offset.height);	
			if (!this.isScroll &&document.all &&!GLOBAL.isIE10() ){
				selectTop-=2;
			}
		}
		optionObj.style.top = selectTop+"px";
		optionObj.style.left = selectLeft+"px";
	},
	/**
	 * 把某一节点的事件转移到其他节点上
	 * */
	changeEvent:function(srcElement,toElement){
		
		var eventTypes = ["onclick"];
		for ( var i = 0 ;i<eventTypes.length;i++ ){
			var clickContent = GLOBAL.dom.getAttribute(srcElement,eventTypes[i]);
			if ( clickContent ){
				GLOBAL.dom.setAttribute(toElement,eventTypes[i], clickContent);
				GLOBAL.dom.setAttribute(srcElement,eventTypes[i], "null");
			}
		}
	},
	/**
	 * 关闭下拉框
	 * */
	closeOption:function(divOption){
		if ( divOption ){
			if ( divOption.style.display != "none" ){
				divOption.style.display = "none";
				GLOBAL.removeEvent(window.document,"click", windowClickHandler);
			}
			
		}
	},
	/**
	 * 打开下拉框
	 * */
	openOption:function(divOption){
		if ( divOption ){
			if ( divOption.style.display == "none" ){
				divOption.style.display = "";
				//宽度自适应
			    this.repeatOptionWidth(divOption);
				//定位位置
				this.repeatPosition(document.getElementById(this.getSelectId()), divOption);
			}
		}
	},
	/**select宽度调整*/
	repeatSelectWidth:function(){
		if( !this.getOptions().selectWidth ){
			var divSelect = document.getElementById(this.getSelectId());
			var selectWidth = 0;
			while( this.getSelectContentDom().scrollWidth +this.getSelectImageDom().scrollWidth+10+"px" !=selectWidth ){
				selectWidth = this.getSelectContentDom().scrollWidth +this.getSelectImageDom().scrollWidth+10+"px";
				divSelect.style.width = selectWidth;
			}
		}
	},
	/**修改下拉框文本内容*/
	changeSelectContent:function(text){
		if( this.getSelectContentText().enable ){
			this.getSelectContentText().dom.value = text;
		}else{
			this.getSelectContentDom().innerHTML = text;
		}
	},
	/**
	 * 选中某个值
	 * */
	optionSelected:function(optionLi){
		if(!optionLi){
			return false;
		}
		optionLi.className = this.selectedClassName;
		var text =GLOBAL.dom.getAttribute(optionLi, this.optionAttr.text);
		var valueAttr = this.optionAttr.value;
		var hiddenDom = this.getHiddenDom();
		//this.getSelectContentDom().innerHTML = text;
		this.changeSelectContent(text);
		this.repeatSelectWidth();
		var value = GLOBAL.dom.getAttribute(optionLi, valueAttr);
		if ( hiddenDom ){
			//获取选中的值写进隐藏域
			if ( value !=null ){
				hiddenDom.value = value;
				if(hiddenDom.name ==""){
					hiddenDom.name = this.getHiddenDomName();
				}
			}
		}
		if( this.getSelectedValue() !=value||this.getSelectedText()!=text){
			this.setOpenChangeFun(true);
		}else{
			this.setOpenChangeFun(false);
		}
		this.setSelectedValue(value);
		this.setSelectedText(text);
		this.setSelectedOptionLiDom(optionLi);
		if( this.getOpenChangeFun() ){
			eval(this.getChangeFun());
		}
	},
	optionUnSelected:function(optionLi){
		optionLi.className = this.unSelectedClassName;
	},
	/**
	 * 增加选项
	 * */
	addOptionLi:function(value,text){
		var optionObj = this;
		var divOption = this.getOptionDom();
		var optionDivData =document.createElement("div");
		optionDivData.innerHTML = text;
		GLOBAL.dom.setAttribute(optionDivData, this.optionAttr.value, value);
		var optionLi = this.createOptionLi(optionDivData,this);
		optionLi.onmouseover = function(){
			this.className = optionObj.selectedClassName;
			var optionList = divOption.childNodes;
        	for ( var i=0 ;i<optionList.length;i++ ){
        		if ( this!=optionList[i] ){
        			optionList[i].onmouseout();
        		}
        	}
		};
		optionLiClick = function(){
			optionObj.optionSelected(this);
			optionObj.closeOption(optionObj.getOptionDom());
		};
		GLOBAL.attachEvent(optionLi, "click", optionLiClick,optionLi);
		optionObj.addOptionUnderLined(optionLi, optionDivData, divOption);
	},  
	/**删除选项*/
	removeOptionLi:function(selectdValue,selectdText){
		var optionLi = this.selectOptionLi(selectdValue,selectdText);
		if ( optionLi ){
			this.getOptionDom().removeChild(optionLi);
		}
		
	},
	/**移动选项到最后位置*/
	moveOptionToEnd:function(selectdValue,selectdText){
		var optionLi = this.selectOptionLi(selectdValue,selectdText);
		if ( optionLi ){
			this.getOptionDom().appendChild(optionLi);
		}
	},
	/**清空option*/
	clearOption:function(){
		this.getOptionDom().innerHTML = "";
	}
	,
	/**查询选项，返回该选项的dom*/
	selectOptionLi:function(selectdValue,selectdText){
		var optionList = this.getOptionDom().childNodes;
		//搜寻选中的列表项
		for ( var i=0 ;i<optionList.length;i++ ){
			var value = GLOBAL.dom.getAttribute(optionList[i],this.optionAttr.value);
			var text = GLOBAL.dom.getAttribute(optionList[i],this.optionAttr.text);
			var key = selectdValue == null? (selectdText==null?false:selectdText):(selectdText==null?value:"all");
			if ( key ){
				if ( key == "all" ){
					if ( (value == selectdValue) && (text == selectdText) ){
						return optionList[i];
					}
				} else {
					if ( (value == selectdValue) || (text == selectdText) ){
						return optionList[i];
					}
				}
			}else{
				return false;
			}
		}
	}
};

/*--------------------------------------------------------------------------------------------以下属于对外接口----------------------------------------------------/
/**装载DIV进行构建成下拉框
 * obj:DIV对象
 * opt:配置参数
 * */
function loadSelect(obj,opt){
	//如果obj为null或者该下拉框已经装载过，则终止
	if( obj == null ){
		return false;
	}else{
		var build = GLOBAL.dom.getAttribute(obj, new DivSelect().selectAttr.build);
		if( build ){
			return false;
		}
	}
	if (opt == null){
		opt = false;
	}
	var divSelect = new DivSelect();
	var divOption = new DivOption();
	//获取参数
	var domSelectType = GLOBAL.dom.getAttribute(obj, divSelect.selectAttr.selectTypeAttr);
	var domSelectWidth = GLOBAL.dom.getAttribute(obj, divSelect.selectAttr.selectWidthAttr);
	var domOptionWidth = GLOBAL.dom.getAttribute(obj, divSelect.selectAttr.optionWidthAttr);
	var domTitle = GLOBAL.dom.getAttribute(obj, divSelect.selectAttr.titleAttr);
	var domScrollSize =GLOBAL.dom.getAttribute(obj, divSelect.selectAttr.scrollSizeAttr);
	var domChangeEvent = GLOBAL.dom.getAttribute(obj, divSelect.selectAttr.onchange);
	var options = {
			selectDom : obj,																
			selectWidth : opt.selectWidth ==undefined? (domSelectWidth == null?null:domSelectWidth):opt.selectWidth,		//select宽度
			optionWidth : opt.optionWidth ==undefined? (domOptionWidth == null?null:domOptionWidth):opt.optionWidth,		//option宽度																				//option宽度
			title : opt.title ==undefined? (domTitle == null?null:domTitle):opt.title,										//下拉框标题
			type : opt.type ==undefined? (domSelectType == null?null:domSelectType):opt.type,								//下拉框类型
			scroll:{																										//滚动条
				size:opt.scrollSize ==undefined? (domScrollSize == null?null:domScrollSize):opt.scrollSize	
			},
			optionHtml:opt.optionHtml ==undefined? null:opt.optionHtml,														//option的自定义html内容
			selectClickFun:opt.selectClickFun==undefined? null:opt.selectClickFun,											//下拉框点击事件
			selectContentText:opt.selectContentText == undefined? null:opt.selectContentText								//开始文本下拉框
	};
	//装载配置参数
	var selectId = obj.id;
	if( selectId == null ||selectId==""){
		selectId = GLOBAL.selectId;
		GLOBAL.selectId+=1;
		selectId += "kSelectId";
		obj.id = selectId;
	}
	divOption.setId(selectId);
	divOption.setOptions(options);
	if( options.scroll.size ){
		divOption.setScroll(options.scroll);
	}
	if( options.optionHtml ){
		divOption.setOptionHtml(options.optionHtml);
	}
	if( options.selectContentText ){
		divOption.setSelectContentText(options.selectContentText);
	}
	divSelect.setOptions(options);
	divSelect.setId(obj.id);
	divOption.setSelectId(divSelect.getId());
	divSelect.setDivOption(divOption);
	if( domChangeEvent){
		divOption.setChangeFun(domChangeEvent);
	}
	//初始化DIV下拉框
	divSelect.init();
	//创建下拉框内容
	divSelect.createContent();
	//初始化下拉框选项
	divSelect.getDivOption().init();
	//创建下拉框事件
	divSelect.createEvent();
	//创建下拉列表
	divSelect.getDivOption().createOption();
	divSelect.optionInitSelected();
	divSelect.getDivOption().getOptionDom().style.display = "none";
	//把select保存起来
	var args = {
			id :divSelect.getId(),
			obj:divSelect
	};
	GLOBAL.select.push(args);
}
/**
 * 自动寻找页面上下拉框进行装载
 * */
function loadSelectAll(){
	var selectList = GLOBAL.dom.getElementsByClassName("kSelect");
	if( selectList ==null ||selectList==undefined ||selectList ==""){
		return false;
	}
	for (var i= selectList.length-1;i<selectList.length,i>-1;i--){
		var id  = selectList[i].id;
		//如果没设id，则给一个id
		if( id == null ||id == "" ){
			id = "kSelectId"+i;
			selectList[i].id = id;
		}
		loadSelect(selectList[i]);
	}
	
}
/***有时候需要重置下拉框选项*/
function resetOption(obj){
	var divSelect = new DivSelect();
	divSelect = getSelectObjFromContext(obj);
	if(divSelect){
		divSelect.optionInitSelected();
	}
}
/**
 * 获取选中的值
 * @param obj 下拉框对象
 * */
function getSelectedValue(obj){
	var divSelect = new DivSelect();
	divSelect = getSelectObjFromContext(obj);
	if(divSelect){
		var value = divSelect.getDivOption().getSelectedValue();
		return value;
	}
}
/**
 * 获取选中的文本
 * @param obj 下拉框对象
 * */
function getSelectedText(obj){
	var divSelect = new DivSelect();
	divSelect = getSelectObjFromContext(obj);
	if(divSelect){
		var text = divSelect.getDivOption().getSelectedText();
		return text;
	}
}
/**
 * 清空option
 * */
function clearOption(obj){
	var divSelect = new DivSelect();
	divSelect = getSelectObjFromContext(obj);
	if(divSelect){
		var value = divSelect.getDivOption().clearOption();
	}
}
/**
 * 下拉框选中
 *@param obj 下拉框对象
 *@param selectdValue 根据值来选中
 *@param selectdText 根据文本来选中
 * */
function kSelectSelected(obj,selectdValue,selectdText){
	var divSelect = new DivSelect();
	divSelect = getSelectObjFromContext(obj);
	if( divSelect ){
		divSelect.getDivOption().optionSelected(divSelect.getDivOption().selectOptionLi(selectdValue, selectdText));
	}
}
/**
 * 添加选项
 *@param obj 下拉框对象
 *@param value 
 *@param text 
 * */
function kSelectAddOption(obj,value,text){
	var divSelect = new DivSelect();
	divSelect = getSelectObjFromContext(obj);
	if( divSelect ){
		divSelect.getDivOption().addOptionLi(value, text);
	}
	
}
/**
 * 删除选项
 *@param obj 下拉框对象
 *@param value 
 *@param text 
 * */
function kSelectRemoveOption(obj,value,text){
	var divSelect = new DivSelect();
	divSelect = getSelectObjFromContext(obj);
	if( divSelect ){
		divSelect.getDivOption().removeOptionLi(value, text);
	}
	
}
/**
 * 移到最后
 *@param obj 下拉框对象
 *@param value 
 *@param text 
 * */
function kSelectMoveOption(obj,value,text){
	var divSelect = new DivSelect();
	divSelect = getSelectObjFromContext(obj);
	if( divSelect ){
		divSelect.getDivOption().moveOptionToEnd(value, text);
	}
	
}
function closeOption(obj){
	var divSelect = new DivSelect();
	divSelect = getSelectObjFromContext(obj);
	if( divSelect ){
		divSelect.getDivOption().closeOption();
	}
}
function getSelectObjFromContext(obj){
	var divSelect = new DivSelect();
	if( obj == null ){
		return false;
	}
	if( obj.id ==null &&obj.id==undefined ){
		return false;
	}
	var selectId =obj.id;
	//获取select对象
	for(var i = 0 ;i<GLOBAL.select.length;i++){
		if ( GLOBAL.select[i].id ==selectId ){
			divSelect  = GLOBAL.select[i].obj;
		}
	}
	if ( divSelect.getId()!= selectId ){
		return false;
	}
	if ( divSelect.getDivOption().getOptionDom() == null ){
		//创建option
		divSelect.getDivOption().createOption();
		divSelect.getDivOption().getOptionDom().style.display = "none";
	}
	return divSelect;
}
